package com.njmaomao.gj.pyot.service.domain;

import com.njmaomao.gj.pyot.common.Systems;
import com.njmaomao.gj.pyot.common.Utils;
import com.njmaomao.gj.pyot.common.bean.Response;
import com.njmaomao.gj.pyot.domain.external.GeneralOrderPayment;
import com.njmaomao.gj.pyot.domain.wechat.PaymentOrder;
import com.njmaomao.gj.pyot.domain.model.PayRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

@Component
public class PaymentOrderService extends HttpService {

    @Value("${external.url.update-order-paid}")
    private String updateOrderPaidUrl;

    @Value("${external.url.query-order-payment}")
    private String queryOrderPaymentUrl;

    private final Logger log = LoggerFactory.getLogger(PaymentOrderService.class);

    public PaymentOrder getOrder(String orderCode, Long customerId) {

        OrderPaymentInfoRequest orderPaymentInfoRequest = new OrderPaymentInfoRequest();
        orderPaymentInfoRequest.setSeqNo(Utils.getSeqNo());
        orderPaymentInfoRequest.setFromSystem(Systems.PAY_YTO.name());
        orderPaymentInfoRequest.setOrderCode(orderCode);
        orderPaymentInfoRequest.setOperator(customerId);

        Response<GeneralOrderPayment> response = post(queryOrderPaymentUrl, orderPaymentInfoRequest, new ParameterizedTypeReference<Response<GeneralOrderPayment>>() {});

       /* ResponseEntity<Response<GeneralOrderPayment>> responseEntity = restTemplate.exchange(
                queryOrderPaymentUrl,
                HttpMethod.POST,
                new HttpEntity<>(orderPaymentInfoRequest, new HttpHeaders()),
                new ParameterizedTypeReference<Response<GeneralOrderPayment>>() {}
        );
        GeneralOrderPayment generalOrderPayment = responseEntity.getBody().getData();*/

        GeneralOrderPayment generalOrderPayment = response.getData();

        return generalOrderPayment;
    }

    /**
     * 更新订单为已支付状态
     * @param payRecord
     * @return
     */
    public boolean updateOrderPaid(PayRecord payRecord){

        OrderPaymentRequest orderPaymentRequest = new OrderPaymentRequest();
        orderPaymentRequest.setSeqNo(Utils.getSeqNo());
        orderPaymentRequest.setOrderCode(payRecord.getOrderCode());
        orderPaymentRequest.setPaymentMethod(1);
        orderPaymentRequest.setPaymentType(1);
        orderPaymentRequest.setExternalPaymentCode(payRecord.getTradeNo());
        orderPaymentRequest.setFromSystem(Systems.PAY_YTO.name());
        orderPaymentRequest.setOperator(payRecord.getCustomerId());

        /*ResponseEntity<Response<Void>> responseEntity = restTemplate.exchange(
                updateOrderPaidUrl,
                HttpMethod.POST,
                new HttpEntity<>(orderPaymentRequest, new HttpHeaders()),
                new ParameterizedTypeReference<Response<Void>>() {}
        );*/
        Response<Void> response = post(
                updateOrderPaidUrl,
                orderPaymentRequest,
                new ParameterizedTypeReference<Response<Void>>() {}
        );
        return response.isSuccess();
    }
}
